/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.core.wicket.component.tree;

import javax.swing.tree.TreeModel;

import com.eelpo.common.util.BlankUtils;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.tree.LabelTree;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.lang.PropertyResolver;

/**
 * <pre>
 * 
 * 菜单树
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class LinkMenuTree extends LabelTree {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7569714926809162108L;

	public LinkMenuTree(String id) {
		super(id);
	}

	public LinkMenuTree(String id, TreeModel model) {
		super(id, model);
	}

	public LinkMenuTree(String id, IModel<TreeModel> model) {
		super(id, model);
	}

	@Override
	@SuppressWarnings("unchecked")
	protected Component newNodeComponent(String id, IModel<Object> model) {

		// 获取菜单Code
		Object code = PropertyResolver.getValue("userObject.code", model.getObject());

		if (BlankUtils.isBlank(code)) {
			return new Label(id, getNodeTextModel(model));
		} else {
			try {
				return new LinkMenuPanel(id, (Class<Page>) Class.forName(code.toString()), model);
			} catch (ClassNotFoundException e) {
				return new Label(id, getNodeTextModel(model));
			}
		}
	}

	@Override
	protected IModel<Object> getNodeTextModel(IModel<Object> nodeModel) {
		return new PropertyModel<Object>(nodeModel.getObject(), "userObject.name");
	}

	public MarkupContainer newLink(String id, final ILinkCallback callback) {
		if (getLinkType() == LinkType.REGULAR) {
			return new Link<Void>(id) {
				private static final long serialVersionUID = 1L;

				/**
				 * @see org.apache.wicket.markup.html.link.Link#onClick()
				 */
				@Override
				public void onClick() {
					callback.onClick(null);
				}

				@Override
				protected boolean getStatelessHint() {
					return true;
				}
			};
		} else if (getLinkType() == LinkType.AJAX) {
			return new AjaxLink<Void>(id) {
				private static final long serialVersionUID = 1L;

				/**
				 * @see org.apache.wicket.ajax.markup.html.AjaxLink#onClick(org.apache.wicket.ajax.AjaxRequestTarget)
				 */
				@Override
				public void onClick(AjaxRequestTarget target) {
					callback.onClick(target);
				}

				@Override
				protected boolean getStatelessHint() {
					return true;
				}
			};
		} else {
			return new AjaxFallbackLink<Void>(id) {
				private static final long serialVersionUID = 1L;

				/**
				 * @see org.apache.wicket.ajax.markup.html.AjaxFallbackLink#onClick(org.apache.wicket.ajax.AjaxRequestTarget)
				 */
				@Override
				public void onClick(AjaxRequestTarget target) {
					callback.onClick(target);
				}

				@Override
				protected boolean getStatelessHint() {
					return true;
				}
			};
		}
	}

	@Override
	protected boolean getStatelessHint() {
		return false;
	}
}
